﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Timers;
using System.Diagnostics;

namespace SensorDetection
{
    class IESensor : Sensor, IObserver
    {
        private bool isEnabled;
        private List<SHDocVw.InternetExplorer> ie=new List<SHDocVw.InternetExplorer>();
        private Timer myTimer;
        private const int TIMER_INTERVAL=5000;
        public IESensor()
        {
            name = "Explorer Sensor";
            isEnabled = false;
            Process[] proc = System.Diagnostics.Process.GetProcesses(Environment.MachineName);
            foreach (Process p in proc)
            {
                if (p.ProcessName.ToString().Equals("iexplore"))
                {
                    isEnabled = true;
                    break;
                }
            }
            if (isEnabled)
            {
                InitializeApp();
            }
        }
        public bool enabled()
        {
            return isEnabled;
        }
        public void populate()
        {
            //repopulates ie with current shellwindows
            SHDocVw.ShellWindows shellwindows = new SHDocVw.ShellWindowsClass();
            ie.Clear();
            foreach (SHDocVw.InternetExplorer i in shellwindows)
            {
                ie.Add(i);
            }
        }
        private void InitializeApp()
        {
            populate();
            //data = ie;
            //Initialize timer
            myTimer = new Timer();
            myTimer.Elapsed += new ElapsedEventHandler(IEEventArrived);
            myTimer.Interval = TIMER_INTERVAL;
            myTimer.Start();
        }
        private void IEEventArrived(object source, ElapsedEventArgs f)
        {
            populate();
            //Make sure contents of data, ie are different so we only fire event when we need to
            if (!ie.Equals((List<SHDocVw.InternetExplorer>)data))
            {
                Console.WriteLine("Notification in iesensor");
                data = ie;
                Console.WriteLine(data.GetType());
                Notify();
            }
        }
        //Designed to be sent only by the processSensor, gives array of running processes
        //Enables this sensor if internet explorer was recently opened
        public void Update(string sender, object msg)
        {
            if (!isEnabled)//only run if sensor is disabled
            {
                Console.WriteLine("Process event detected from IE class");
                Process[] proc = (Process[])msg;
                bool enableThis = false;
                foreach (Process p in proc)
                {
                    if (p.ProcessName.ToString().Equals("iexplore"))
                    {
                        enableThis = true;
                        Console.WriteLine("Internet explorer Opened");
                        InitializeApp();
                        break;
                    }
                }
                isEnabled = enableThis;
            }
        }
    }
}
